package com.wwtech.epp.ui.adapter.main;

import android.support.annotation.Nullable;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.socks.library.KLog;
import com.wwtech.epp.R;
import com.wwtech.epp.db.Friend;
import com.wwtech.epp.view.GlideCircleTransform;

import java.util.List;

/**
 * Created by hu on 2017/9/13.
 */

public class GroupMemberAdapter extends BaseQuickAdapter<Friend, BaseViewHolder> {

    private boolean canDelet = true;

    public boolean isCanDelet() {
        return canDelet;
    }

    public void setCanDelet(boolean canDelet) {
        this.canDelet = canDelet;
    }

    private OnCheckChange onCheckChange;

    public GroupMemberAdapter(@Nullable List<Friend> data) {
        super(R.layout.check_person_item, data);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, Friend friend) {
        ImageView imageView = baseViewHolder.getView(R.id.avater);
        CheckBox checkBox = baseViewHolder.getView(R.id.check_box);
        Glide.with(mContext)
                .load(friend.getAvatar())
                .transform(new GlideCircleTransform(mContext))
                .into(imageView);
        baseViewHolder.setText(R.id.name, friend.getNickname());

        if (!canDelet) {
            if (friend.isCanCheck()) {
                checkBox.setEnabled(true);
            } else {
                KLog.i("适配器中，设置不能被选中");
                checkBox.setEnabled(false);
            }
        } else {
            checkBox.setEnabled(true);
        }
        baseViewHolder.setOnCheckedChangeListener(R.id.check_box, new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                onCheckChange.onCheckChange(friend, isChecked);
            }
        });
        if (friend.getChecked()) {
            KLog.i("选中" + friend.getNickname());
            checkBox.setChecked(true);
        } else {
            checkBox.setChecked(false);
        }
//        }
    }

    public interface OnCheckChange {
        void onCheckChange(Friend friend, boolean isChecked);
    }

    public void setOncheckChange(OnCheckChange oncheckChange) {
        this.onCheckChange = oncheckChange;
    }
}
